/* $Header: vx_cmds.c,v 2.2 01/10/02 10:29:26 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME: vx_cmds.c                                              */
/* ============                                                        */
/*                                                                     */
/* Author:  Bob Cook                                                   */
/*                                                                     */
/* MODULE PURPOSE: This module will handle the valves commands.        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>

#include <error.h>

#include <mio.h>
#include <cmd_set.h>
#include <cmdproc.h>
#include <method.h>
#include <hostutil.h>
#include <cmd_tbls.h>
#include <sequence.h>
#include <seq_ui.h>


/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/



/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/

/* if BC_DEBUG is defined debug buffers are created and statics become global */
#define BC_DEBUG

/* when debugging make variables global for emulator testing */
#ifdef BC_DEBUG
#   define SCOPE
#else
#   define  SCOPE  static
#endif


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   MACROS                  */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

/***********************************************************************/
/* FUNCTION: DoVXEO                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Echo command.                                              */
/*                                                                     */
/* RETURNED VALUE: length of response string                           */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

INT16 DoVXEO(CMD_TYPE *p_cmd,BIT8  *p_resp_parm)
{
U_INT16 len;

   len = 0;

   if (p_cmd->parmv[0] == 1)
   {
      len = PutAsciiParam((BIT8 *)"\"", p_resp_parm);
      len += PutAsciiParam((BIT8 *)&(p_cmd->parms[p_cmd->parmv[1]]),
				     p_resp_parm+len);
      len += PutAsciiParam((BIT8 *)"\"", p_resp_parm+len);
      return len;
   }
   else
   {
      CmdErrorHandler(p_cmd,1,INVALID_PARAM);
      return NO_RESPONSE;
   }
}

/***********************************************************************/
/* END FUNCTION: DoVXEO                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoVXGI                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Initiate GSV injection sequence.                           */
/*                                                                     */
/* RETURNED VALUE: length of response string                           */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

INT16 DoVXGI(CMD_TYPE *p_cmd,BIT8  *p_resp_parm)
{
UI_ERR error;
INT32 valve1,valve2,nordy;
U_INT8 param_num;

   valve1 = 0;
   valve2 = 0;
   nordy  = 0;
   switch (p_cmd->parmv[0])
   {
   case 0:
      error = MISSING_PARAM;
      param_num = 1;
      break;
   case 1:
      error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[1]]),
			    0,&valve1);
      param_num = 1;
      break;
   case 2:
      error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[1]]),
			    0,&valve1);
      param_num = 1;
      if(!error)
      {
         error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[2]]),
                   0,&valve2);
         param_num = 2;
      }
      if (valve2 == -1 )
      {
         valve2 = 0 ;
         nordy  = -1 ;
      };
      break;
   case 3:
      error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[1]]),
			    0,&valve1);
      param_num = 1;
      if(!error)
      {
         error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[2]]),
                   0,&valve2);
         param_num = 2;
      }
      if(!error)
      {
         error = (UI_ERR)DecToInt(&(p_cmd->parms[p_cmd->parmv[3]]),
                   0,&nordy );
         if ( (!error) && (( nordy < -1 ) || (nordy > 0)) ) error = INVALID_PARAM ;
         
         param_num = 3;
      }
      break;
   default:
      error = INVALID_PARAM ;
      param_num = 4;
      break;
   }
   if(error)
   {
      /* command not formed properly - set error log and ignore */
      CmdErrorHandler( p_cmd, param_num, error);
      return NO_RESPONSE;
   }

   /* formed properly but may still have invalid number - return error */
   error = StartGsvInjection((U_INT8)valve1,(U_INT8)valve2,(U_INT8)nordy);
   return PutIntParam((INT32)error,p_resp_parm,0);
}

/***********************************************************************/
/* END FUNCTION: DoVXGI                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoVXGA                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Abort current GSV injection. Moves the GSV State to idle.  */
/*                                                                     */
/* RETURNED VALUE: length of response string                           */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


INT16 DoVXGA(CMD_TYPE *p_cmd,BIT8  *p_resp_parm)
{
   (void)p_cmd;
   StopGsvInjection();
   return PutIntParam((INT32)OK,p_resp_parm,0);
}

/***********************************************************************/
/* END FUNCTION: DoVXGA                                                */
/***********************************************************************/




/***********************************************************************/
/* FUNCTION: VXOpcodes                                                 */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: calls routines needed to implement the VX commands.        */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* FILE SCOPE STATICS USED:                                            */
/*                                                                     */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

INT16 VXOpcodes(CMD_TYPE  *p_cmd,BIT8  *p_resp_parm,U_INT16 max_resp_len)
{
INT16           parm_len;

   (void) max_resp_len;

   parm_len = NO_RESPONSE;

   switch( p_cmd->opcode.int_op )
   {
      case  MAKEINT('E','O'):
	parm_len = DoVXEO(p_cmd,p_resp_parm);
	break;

      case  MAKEINT('S','W'):
        switch ( p_cmd->dsap.char_dsap[1] ) 
        {
        case '1':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV1SW);
	   break;
        case '2':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV2SW);
	   break;
        case '3':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV3SW);
	   break;
        case '4':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV4SW);
	   break;
        case '5':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV5SW);
	   break;
        case '6':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV6SW);
	   break;
        case '7':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV7SW);
	   break;
        case '8':
	   parm_len = DoCmd(p_cmd,p_resp_parm,&mV8SW);
	   break;
        }
	break;

      case  MAKEINT('G','S'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV1GS);
           break;
        case '2':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV2GS);
           break;
        case '3':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV3GS);
           break;
        case '4':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV4GS);
           break;
        case '5':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV5GS);
           break;
        case '6':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV6GS);
           break;
        case '7':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV7GS);
           break;
        case '8':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV8GS);
           break;
        }
	break;

      case  MAKEINT('M','P'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV1MP);
           break;
        case '2':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV2MP);
           break;
        case '3':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV3MP);
           break;
        case '4':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV4MP);
           break;
        case '5':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV5MP);
           break;
        case '6':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV6MP);
           break;
        case '7':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV7MP);
           break;
        case '8':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV8MP);
           break;
        }
	break;

      case  MAKEINT('C','F'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV1CF);
           break;
        case '2':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV2CF);
           break;
        case '3':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV3CF);
           break;
        case '4':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV4CF);
           break;
        case '5':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV5CF);
           break;
        case '6':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV6CF);
           break;
        case '7':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV7CF);
           break;
        case '8':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV8CF);
           break;
        }
	break;

      case  MAKEINT('C','W'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV1CW);
           break;
        case '2':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV2CW);
           break;
        case '3':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV3CW);
           break;
        case '4':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV4CW);
           break;
        case '5':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV5CW);
           break;
        case '6':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV6CW);
           break;
        case '7':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV7CW);
           break;
        case '8':
           parm_len = DoCmd(p_cmd,p_resp_parm,&mV8CW);
           break;
        }
	break;
      case  MAKEINT('S','T'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
	   parm_len = NO_RESPONSE;
	   CmdErrorHandler(p_cmd,0,INVALID_DEST);
           break;
        case 'X':
           parm_len = DoStatus(p_cmd,p_resp_parm,&mVXST);
           break;
        }
	break;

      case  MAKEINT('G','I'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
	   parm_len = NO_RESPONSE;
	   CmdErrorHandler(p_cmd,0,INVALID_DEST);
           break;
        case 'X':
	   parm_len = DoVXGI(p_cmd,p_resp_parm);
           break;
        }
        break;

      case  MAKEINT('G','A'):
        switch ( p_cmd->dsap.char_dsap[1] )
        {
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
           parm_len = NO_RESPONSE;
           CmdErrorHandler(p_cmd,0,INVALID_DEST);
           break;
        case 'X':
           parm_len = DoVXGA(p_cmd,p_resp_parm);
           break;
        }
        break;


      default:
	 parm_len = NO_RESPONSE;
	 CmdErrorHandler(p_cmd,0,INVALID_OP);
	 break;
   }

   return parm_len;
}


/***********************************************************************/
/* END FUNCTION: VXOpcodes                                             */
/***********************************************************************/



/***********************************************************************/
/* FUNCTION: VX_Cmds                                                   */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: calls function which executes a command if one is          */
/*          available on the command queue.                            */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* FILE SCOPE STATICS USED:                                            */
/*                                                                     */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

void VX_Cmds(void)
{
   ExecCmds(&VX_Cmd_Q,VXOpcodes);
}

/***********************************************************************/
/* END FUNCTION: VX_Cmds                                               */
/***********************************************************************/

